<template>
  <div class="login-container">
    <canvas id="root" width="1000" height="560" class="art-canvas"/>
    <el-form ref="loginForm" :model="loginForm" :rules="loginRules" class="login-form" >
      <el-form-item prop="username">
        <el-input
          v-model="loginForm.username"
          :placeholder="$t('login.username')"
          type="text"
          auto-complete="off"
          class="login-input user"

        />
        <span class="svg-container">
          <svg-icon icon-class="user" />
        </span>
      </el-form-item>
      <el-form-item prop="password">
        <el-input
          v-model="loginForm.password"
          :placeholder="$t('login.password')"
          :type="passwordType"
          auto-complete="off"
          class="login-input user"
        />
        <span class="svg-container">
          <svg-icon icon-class="password" />
        </span>
        <span class="show-pwd" @click="showPwd">
          <svg-icon icon-class="eye" />
        </span>
      </el-form-item>
      <el-form-item>
        <el-button id="login" :loading="loading" class="login-btn" @click="handleLogin">{{ $t('login.logIn') }}</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import { isvalidUsername } from '@/utils/validate'
import LangSelect from '@/components/LangSelect'
import SocialSign from './socialsignin'
import CanvasAnimate from './canvas'
export default {
  name: 'Login',
  components: { LangSelect, SocialSign },
  data() {
    return {
      loginForm: {
        username: 'admin',
        password: ''
      },
      loginRules: {
        username: [{ required: true, trigger: 'blur', validator: (rule, value, callback) => {
          if (!isvalidUsername(value)) {
            callback(new Error('请输入账号'))
          } else {
            callback()
          }
        } }],
        password: [{ required: true, trigger: 'blur', validator: (rule, value, callback) => {
          if (value.length < 6) {
            callback(new Error('请输6位密码'))
          } else {
            callback()
          }
        } }]
      },
      passwordType: 'password',
      loading: false,
      showDialog: false,
      redirect: undefined
    }
  },
  watch: {
    $route: {
      handler: function(route) {
        this.redirect = route.query && route.query.redirect
      },
      immediate: true
    }

  },
  created() {
  },
  mounted() {
    this.canvas()
  },
  methods: {
    showPwd() {
      if (this.passwordType === 'password') {
        this.passwordType = ''
      } else {
        this.passwordType = 'password'
      }
    },
    handleLogin() {
      this.$refs.loginForm.validate(valid => {
        if (valid) {
          this.loading = true
          this.$store.dispatch('LoginByUsername', this.loginForm).then(() => {
            this.loading = false
            this.$router.push({ path: this.redirect || '/' })
          }).catch(() => {
            this.loading = false
          })
        } else {
          console.log('error submit!!')
          return false
        }
      })
    },
    canvas() {
      // 画布背景
      var root = document.querySelector('#root')
      var a = new CanvasAnimate(root, {
        length: 15,
        clicked: false,
        moveon: true
      })
      a.Run()
    }
  }
}
</script>

<style rel="stylesheet/scss" lang="scss" scoped>
$bg:#2d3a4b;
$dark_gray:#889aa4;
$light_gray:#eee;
.login-container {
  position: fixed;
  height: 100%;
  width: 100%;
  background-color: $bg;
  .login-form {
    position: absolute;
    left: 0;
    right: 0;
    width: 400px;
    max-width: 100%;
    padding: 35px 35px 15px 35px;
    margin: 0px auto;
  }
  .tips {
    font-size: 14px;
    color: #fff;
    margin-bottom: 10px;
    span {
      &:first-of-type {
        margin-right: 16px;
      }
    }
  }
  .svg-container {
    padding: 6px 5px 6px 15px;
    color: $dark_gray;
    vertical-align: middle;
    width: 30px;
    display: inline-block;
  }
  .title-container {
    position: relative;
    .title {
      font-size: 26px;
      color: $light_gray;
      margin: 0px auto 40px auto;
      text-align: center;
      font-weight: bold;
    }
    .set-language {
      color: #fff;
      position: absolute;
      top: 5px;
      right: 0px;
    }
  }
  .show-pwd {
    position: absolute;
    right: 12px;
    top: 8px;
    font-size: 16px;
    color: $dark_gray;
    cursor: pointer;
    user-select: none;
  }
  .thirdparty-button {
    position: absolute;
    right: 35px;
    bottom: 28px;
  }
}

.login-container{
    position: absolute;
    width: 100%;
    height: 100%;
    background: #188ae2;

}
.login-inner-box{
     position:relative;
     max-width: 1000px;
     width: 100%;
     height: 100%;
     margin: 0 auto;
    text-align: center;
}

.art-canvas{
    position: absolute;
     top: 50%;;
      left: 50%;
      margin-top: -280px;
      margin-left: -500px
}

.login-form{
    width: 1000px;
    height: 310px;
    position: absolute;
      top: 30%;
      left: 50%;

      margin-left: -500px
}

.login-input{
   border-radius:44px;
   overflow: hidden;

}

.login-btn{
    line-height: 1;
    padding: 0;
    background: #ebcd41;
    color: #000;
    width:100%;
    height: 44px;
    line-height: 44px;
    border-radius: 30px;
   overflow: hidden;
}
.login-btn:hover{
    background: #fee300 !important;
     color: #000 !important;
}
.login-btn:focus{
     background: #fee300 !important;
     color: #000 !important;
}

.svg-container{
  position:absolute;
  left:0;
      top: -2px;
  z-index:99;
}
</style>
<style rel="stylesheet/scss" lang="scss">
  .login-input input{
      border-radius:44px!important;
      height: 44px!important;
     line-height: 44px!important;
     padding: 0 40px!important;
  }

</style>
